#include "sound.h"

#ifndef SKYID_DISABLE_SOUND
namespace SkyID
{

//- Sound ---------------------------------------------------------------------

Sound::Sound():
	mFormat(0), mFrequency(0), mBuffer(NULL)
{
	alGenSources(1, &mID);
	setValid(mID != 0);
	alSourcef(mID, AL_PITCH, 1.0f);
	alSourcef(mID, AL_GAIN,  1.0f);
}

Sound::~Sound()
{
	alDeleteSources(1, &mID);
}

void Sound::play()
{
	alSourcePlay(mID);
}

void Sound::stop()
{
	alSourceStop(mID);
}

void Sound::pause()
{
	alSourcePause(mID);
}

bool Sound::isPlaying() const
{
	ALenum state;
	alGetSourcei(mID, AL_SOURCE_STATE, &state);
	return (state == AL_PLAYING);
}

bool Sound::isPaused() const
{
	ALenum state;
	alGetSourcei(mID, AL_SOURCE_STATE, &state);
	return (state == AL_PAUSED);
}

bool Sound::check() const
{
	ALenum error = alGetError();
	if (error != AL_NO_ERROR)
	{
		LOG_MSG("Sound: %s (%d)", alGetString(error), error);
		return false;
	}
	return true;
}

//- Common --------------------------------------------------------------------

size_t HelperOggRead(void *ptr, size_t size, size_t nmemb, void *datasource)
{
	Reader *reader = reinterpret_cast<Reader*>(datasource);
	LOG_ASSERT(reader);
	return reader->read(ptr, size*nmemb);
}

} // namespace SkyID
#endif // SKYID_DISABLE_SOUND
